package cn.gz3c.uitest.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Vibrator;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.LifecycleOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.hms.audioeditor.sdk.asset.HAEAsset;
import com.huawei.hms.audioeditor.sdk.asset.HAEAudioAsset;
import com.huawei.hms.audioeditor.sdk.lane.HAEAudioLane;
import com.huawei.hms.audioeditor.sdk.lane.HAELane;
import com.huawei.hms.audioeditor.ui.editor.trackview.view.TrackViewFrameLayout;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.gz3c.uitest.viewmodel.EditPreviewViewModel;
import cn.gz3c.uitest.t;
import cn.gz3c.uitest.view.adapter.MainLineRecyclerViewAdapter;


public class MainRecyclerView extends RecyclerView {
    private final Context context;
    private EditPreviewViewModel editPreviewViewModel;
    private long c = 0L;
    private int d = 4;
    private double e = com.huawei.hms.audioeditor.ui.common.utils.a.a(120.0F);
    private boolean f;
    private BaseTrackView baseTrackView;
    private float h;
    private float i;
    private float j;
    private float k = -1.0F;
    private float l;
    private TrackViewFrameLayout trackViewFrameLayout;
    private final List<Time> n = new ArrayList<>();
    private final int o = com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F);
    private long p;
    private MainHorizontalScrollView q;
    int r = 0;

    public MainRecyclerView(@NonNull Context var1) {
        super(var1);
        this.context = var1;
    }

    public MainRecyclerView(@NonNull Context var1, @Nullable AttributeSet var2) {
        super(var1, var2);
        this.context = var1;
    }

    public MainRecyclerView(@NonNull Context var1, @Nullable AttributeSet var2, int var3) {
        super(var1, var2, var3);
        this.context = var1;
    }

    @SuppressLint({"MissingPermission", "WrongConstant"})
    private void a() {
        Vibrator var1;
        if (com.huawei.hms.audioeditor.ui.common.utils.a.a(this.context, "android.permission.VIBRATE") && (var1 = (Vibrator) this.context.getSystemService("vibrator")) != null) {
            var1.vibrate(80L);
        }

    }

    private double b(float var1) {
        return (double) this.baseTrackView.l() / com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * this.e + (double) var1;
    }

    private double a(float var1) {
        return (double) this.baseTrackView.c() / com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * this.e + (double) var1;
    }

    private void a(MotionEvent var1, int var2, long var3) {
        this.baseTrackView.b(var1.getX() - this.h);
        this.k = (float) var2;
        this.p = var3 - this.baseTrackView.i();
        this.r = 1;
        this.a();
    }

    private int a(long var1) {
        return (int) ((double) var1 / com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * this.e);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        int var2;
        if ((var2 = motionEvent.getAction() & motionEvent.getActionMasked()) != 0) {
            if (var2 != MotionEvent.ACTION_UP) {
                if (var2 == MotionEvent.ACTION_MOVE) {
                    if (this.f) {
                        this.editPreviewViewModel.setLdUuid("");
                        this.editPreviewViewModel.b(1);
                        BaseTrackView baseTrackView;
                        if ((baseTrackView = this.baseTrackView) != null && baseTrackView.getParent() instanceof TrackViewFrameLayout) {
                            var2 = (int) (motionEvent.getX() - this.h);
                            View var3;
                            View var10000 = var3 = this.findChildViewUnder(motionEvent.getX(), motionEvent.getY());
                            TrackViewFrameLayout var4 = (TrackViewFrameLayout) this.baseTrackView.getParent();
                            int var5;
                            if (var10000 instanceof TrackViewFrameLayout) {
                                this.trackViewFrameLayout = (TrackViewFrameLayout) var3;
                                if (this.trackViewFrameLayout.a() != var4.a() && this.editPreviewViewModel.b().size() > 1) {
                                    this.a();
                                    var4.removeView(this.baseTrackView);
                                    this.trackViewFrameLayout.addView(this.baseTrackView);
                                }
                            } else {
                                float var10 = var4.getY() + (float) var4.getHeight();
                                if (motionEvent.getY() > var10 && var4.a() != -1) {
                                    MainLineRecyclerViewAdapter var12;
                                    t var14;
                                    ArrayList var18;
                                    List var21;
                                    if ((var5 = (var14 = (var12 = (MainLineRecyclerViewAdapter) this.getAdapter()).b).b()) != 1) {
                                        if (var5 == 2) {
                                            var21 = var14.c().b;
                                            var18 = new ArrayList();
                                            int var6 = var14.b();
                                            String var7 = this.editPreviewViewModel.getLdUuid().getValue();
                                            var21.add(new t.b(-1, var18, var6, var7));
                                        }
                                    } else {
                                        var21 = var14.c().a;
                                        var18 = new ArrayList();
                                        var21.add(new  t.b(-1, var18, 1));
                                    }

                                    var12.notifyItemInserted(var14.a().size() - 1);
                                }
                            }

                            if (!(this.b((float) var2) < 0.0D)) {
//                                SmartLog.i("handleAdsorb", com.huawei.hms.audioeditor.ui.p.a.a("x: ").append(motionEvent.getX()).append(" oldX: ").append(this.j).toString());
//                                SmartLog.i("handleAdsorb", "assetLocations: " + this.n.toString());
//                                SmartLog.i("handleAdsorb", "dragView.getStartX(): " + this.baseTrackView.m());
                                double var9;
                                if (this.r == 0) {
                                    this.baseTrackView.b(motionEvent.getX() - this.h);
                                    var9 = (double) this.baseTrackView.m() / this.e;
                                    this.p = (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * var9);
                                }

                                Time var13;
                                Iterator var15;
                                int var17;
                                double var19;
                                if (motionEvent.getX() < this.j) {
                                    float var11;
                                    if (this.r == -1 && ((var11 = this.k) < 0.0F || (double) var11 - this.b(motionEvent.getX() - this.h) > (double) this.o)) {
                                        this.r = 0;
                                        this.k = -1.0F;
                                        this.baseTrackView.b(motionEvent.getX() - this.h);
                                        var9 = (double) this.baseTrackView.m() / this.e;
                                        this.p = (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * var9);
                                    }

                                    if (this.r == 1 && (double) this.k - this.a(motionEvent.getX() - this.h) > (double) this.o) {
                                        this.r = 0;
                                        this.k = -1.0F;
                                        this.baseTrackView.b(motionEvent.getX() - this.h);
                                        var9 = (double) this.baseTrackView.m() / this.e;
                                        this.p = (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * var9);
                                    }

                                    var15 = this.n.iterator();

                                    while (var15.hasNext()) {
                                        var13 = (Time) var15.next();
                                        if ((var17 = this.baseTrackView.m() - var13.startTimeInt) > 0 && var17 < this.o) {
                                            this.h += (float) var17;
                                            this.baseTrackView.b(motionEvent.getX() - this.h);
                                            this.k = (float) var13.startTimeInt;
                                            this.p = var13.startTime;
                                            this.r = -1;
                                            this.a();
                                            break;
                                        }

                                        if ((var17 = this.baseTrackView.m() - var13.endTimeInt) > 0 && var17 < this.o) {
                                            this.h += (float) var17;
                                            this.baseTrackView.b(motionEvent.getX() - this.h);
                                            this.k = (float) var13.endTimeInt;
                                            this.p = var13.endTime;
                                            this.r = -1;
                                            this.a();
                                            break;
                                        }

                                        var19 = this.baseTrackView.m();
                                        if ((var5 = (int) (this.baseTrackView.j() + var19 - (double) (var17 = var13.startTimeInt))) > 0 && var5 < this.o) {
                                            this.h += (float) var5;
                                            this.a(motionEvent, var17, var13.startTime);
                                            break;
                                        }

                                        var19 = this.baseTrackView.m();
                                        if ((var5 = (int) (this.baseTrackView.j() + var19 - (double) (var17 = var13.endTimeInt))) > 0 && var5 < this.o) {
                                            this.h += (float) var5;
                                            this.a(motionEvent, var17, var13.endTime);
                                            break;
                                        }
                                    }
                                }

                                if (motionEvent.getX() > this.j) {
                                    if (this.r == -1 && (this.k < 0.0F || this.b(motionEvent.getX() - this.h) - (double) this.k > (double) this.o)) {
                                        this.k = -1.0F;
                                        this.r = 0;
                                        this.baseTrackView.b(motionEvent.getX() - this.h);
                                        var9 = (double) this.baseTrackView.m() / this.e;
                                        this.p = (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * var9);
                                    }

                                    if (this.r == 1 && this.a(motionEvent.getX() - this.h) - (double) this.k > (double) this.o) {
                                        this.k = -1.0F;
                                        this.r = 0;
                                        this.baseTrackView.b(motionEvent.getX() - this.h);
                                        var9 = (double) this.baseTrackView.m() / this.e;
                                        this.p = (long) (com.huawei.hms.audioeditor.ui.common.utils.d.a(this.d) * var9);
                                    }

                                    var15 = this.n.iterator();

                                    while (var15.hasNext()) {
                                        if ((var17 = (var13 = (Time) var15.next()).startTimeInt - this.baseTrackView.m()) > 0 && var17 < com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F)) {
                                            this.h -= (float) var17;
                                            this.baseTrackView.b(motionEvent.getX() - this.h);
                                            this.k = (float) var13.startTimeInt;
                                            this.p = var13.startTime;
                                            this.r = -1;
                                            this.a();
                                            break;
                                        }

                                        if ((var17 = var13.endTimeInt - this.baseTrackView.m()) > 0 && var17 < com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F)) {
                                            this.h -= (float) var17;
                                            this.baseTrackView.b(motionEvent.getX() - this.h);
                                            this.k = (float) var13.endTimeInt;
                                            this.p = var13.endTime;
                                            this.r = -1;
                                            this.a();
                                            break;
                                        }

                                        if ((var17 = (int) ((double) (var13.startTimeInt - this.baseTrackView.m()) - this.baseTrackView.j())) > 0 && var17 < com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F)) {
                                            this.h -= (float) var17;
                                            this.a(motionEvent, var13.startTimeInt, var13.startTime);
                                            break;
                                        }

                                        if ((var17 = (int) ((double) (var13.endTimeInt - this.baseTrackView.m()) - this.baseTrackView.j())) > 0 && var17 < com.huawei.hms.audioeditor.ui.common.utils.a.a(3.0F)) {
                                            this.h -= (float) var17;
                                            this.a(motionEvent, var13.endTimeInt, var13.endTime);
                                            break;
                                        }
                                    }
                                }

                                this.j = motionEvent.getX();
                                if (this.getParent().getParent() instanceof MainHorizontalScrollView) {
                                    this.q = (MainHorizontalScrollView) this.getParent().getParent();
                                    int var10001 = com.huawei.hms.audioeditor.ui.common.utils.g.b(this.context);
                                    var9 = com.huawei.hms.audioeditor.ui.common.utils.a.a((float) var10001, 8.0F);
                                    var19 = (double) var10001 - var9;
                                    if (com.huawei.hms.audioeditor.ui.common.utils.a.a(motionEvent.getRawX(), (double) this.i) && com.huawei.hms.audioeditor.ui.common.utils.a.a(motionEvent.getRawX(), var19)) {
                                        var2 = (int) com.huawei.hms.audioeditor.ui.common.utils.a.a(com.huawei.hms.audioeditor.ui.common.utils.a.e(motionEvent.getRawX(), var19), 0);
                                        this.q.scrollX(var2);
                                    } else if (com.huawei.hms.audioeditor.ui.common.utils.a.a(this.i, (double) motionEvent.getRawX()) && com.huawei.hms.audioeditor.ui.common.utils.a.a(var9, motionEvent.getRawX())) {
                                        var2 = (int) com.huawei.hms.audioeditor.ui.common.utils.a.a(com.huawei.hms.audioeditor.ui.common.utils.a.e(motionEvent.getRawX(), var9), 0);
                                        this.q.scrollX(var2);
                                    } else {
//                                        SmartLog.i("xxxxxx", com.huawei.hms.audioeditor.ui.p.a.a("else: startScrollX:").append(this.i).append("event.getRawX():").append(motionEvent.getRawX()).toString());
                                    }
                                }
                            }
                        }
                    }

//                    SmartLog.i("TAG", com.huawei.hms.audioeditor.ui.p.a.a("ACTION_MOVE2：").append(motionEvent.getX(0)).toString());
                    if (motionEvent.getPointerCount() >= 2) {
                        return false;
                    }
                }
            } else if (this.f) {
                label201:
                {
                    this.editPreviewViewModel.c("");
                    if (this.baseTrackView.a() != null) {
                        if (this.p < 0L) {
                            this.editPreviewViewModel.K();
                            break label201;
                        }

                        if (this.baseTrackView.a().getType() != HAEAsset.HAEAssetType.AUDIO) {
                            break label201;
                        }

                        HAELane.HAELaneType var20 = HAELane.HAELaneType.AUDIO;
                        TrackViewFrameLayout var16;
                        if ((var16 = this.trackViewFrameLayout) != null) {
                            if (var16.a() == -1) {
                                if (this.editPreviewViewModel.b().size() > 1) {
                                    this.editPreviewViewModel.a(var20, this.baseTrackView.a().getLaneIndex(), this.baseTrackView.a().getIndex(), this.p);
                                }
                            } else {
                                this.editPreviewViewModel.a(var20, this.baseTrackView.a().getLaneIndex(), this.baseTrackView.a().getIndex(), this.trackViewFrameLayout.a(), this.p);
                            }
                        }
                    }

                    this.editPreviewViewModel.K();
                    this.editPreviewViewModel.J();
                }

                this.f = false;
                this.editPreviewViewModel.a(false);
            } else if (Math.abs(motionEvent.getY() - this.l) < 20.0F) {
                this.editPreviewViewModel.setLdUuid("");
                this.editPreviewViewModel.b(1);
            }
        } else {
            this.h = motionEvent.getX();
            this.j = motionEvent.getX();
            this.i = motionEvent.getRawX();
            this.l = motionEvent.getY();
        }

        return super.dispatchTouchEvent(motionEvent);
    }

    public void a(EditPreviewViewModel var1) {
        this.editPreviewViewModel = var1;
        var1.i().observe((LifecycleOwner) this.context, (var1x) -> {
            this.c = var1x;
        });
        var1.m().observe((LifecycleOwner) this.context, (var1x) -> {
            this.d = var1x;
        });
        var1.n().observe((LifecycleOwner) this.context, (var1x) -> {
            this.e = var1x;
        });
        var1.j().observe((LifecycleOwner) this.context, (var1x) -> {
            boolean var2;
            var2 = !com.huawei.hms.audioeditor.ui.common.utils.a.a(var1x);

            this.f = var2;
            if (this.f) {
                for (int var8 = 0; var8 < this.getChildCount(); ++var8) {
                    View var3;
                    if ((var3 = this.getChildAt(var8)) instanceof TrackViewFrameLayout) {
                        TrackViewFrameLayout var9 = (TrackViewFrameLayout) var3;

                        for (int var4 = 0; var4 < var9.getChildCount(); ++var4) {
                            BaseTrackView var5;
                            if ((var5 = (BaseTrackView) var9.getChildAt(var4)) != null && var1x.equals(var5.o())) {
                                this.baseTrackView = var5;
                                this.p = this.baseTrackView.l();
                                if (this.baseTrackView != null) {
                                    this.n.clear();
                                    List var10001 = this.n;
                                    int var10004 = this.a(this.c);
                                    int var10005 = this.a(this.c);
                                    long var10006 = this.c;
                                    var10001.add(new Time(var10004, var10005, var10006, var10006));
                                    Iterator var7 = this.editPreviewViewModel.G().getAllAudioLane().iterator();

                                    Iterator var10;
                                    HAEAsset var11;
                                    label68:
                                    while (var7.hasNext()) {
                                        var10 = ((HAEAudioLane) var7.next()).getAssets().iterator();

                                        while (true) {
                                            do {
                                                if (!var10.hasNext()) {
                                                    continue label68;
                                                }
                                            } while (!((var11 = (HAEAsset) var10.next()) instanceof HAEAudioAsset));

                                            for (Float aFloat : ((HAEAudioAsset) var11).getFootPrintList()) {
                                                long var13 = aFloat.longValue();
                                                var13 += var11.getStartTime();
                                                this.n.add(new Time(this.a(var13), this.a(var13), var13, var13));
                                            }
                                        }
                                    }

                                    if (this.baseTrackView.a() != null && this.baseTrackView.a().getType() == HAEAsset.HAEAssetType.AUDIO) {
                                        var7 = this.editPreviewViewModel.G().getAllAudioLane().iterator();

                                        while (var7.hasNext()) {
                                            var10 = ((HAEAudioLane) var7.next()).getAssets().iterator();

                                            while (var10.hasNext()) {
                                                if (!(var11 = (HAEAsset) var10.next()).getUuid().equals(this.baseTrackView.o())) {
                                                    this.n.add(new Time(this.a(var11.getStartTime()), this.a(var11.getEndTime()), var11.getStartTime(), var11.getEndTime()));
                                                }
                                            }
                                        }
                                    }
                                }

                                return;
                            }
                        }
                    }
                }
            }

        });
    }

    private static class Time {
        public int startTimeInt;
        public int endTimeInt;
        public long startTime;
        public long endTime;

        public Time(int startTimeInt, int endTimeInt, long startTime, long endTime) {
            this.startTimeInt = startTimeInt;
            this.endTimeInt = endTimeInt;
            this.startTime = startTime;
            this.endTime = endTime;
        }
    }
}
